package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuandian.api.management.entity.ConfNodeProperty;
import com.yuandian.api.management.validate.SelectFlag;
import com.yuandian.api.management.validate.UpdateFlag;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.management.service.ConfNodePropertyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.groups.Default;
import java.util.List;

/**
 * 节点属性配置表
 * @author zfsn
 * @date 2023-4-14
 */
@Slf4j
@Api(tags = "节点属性配置(远程调用接口)")
@RestController
@RequestMapping("/nodeProperty")
@RequiredArgsConstructor
public class ConfNodePropertyController {

	private final ConfNodePropertyService confNodePropertyService;



	/**
	 * 节点位置坐标配置
	 * @param confNodeProperty
	 * @return
	 */
	@PostMapping("/setNodePosition")
	public R<Boolean> setNodePosition(@Validated({UpdateFlag.class, Default.class}) @RequestBody ConfNodeProperty confNodeProperty) {
		LambdaQueryWrapper<ConfNodeProperty> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(ConfNodeProperty::getNodeId, confNodeProperty.getNodeId());
		queryWrapper.eq(ConfNodeProperty::getType, confNodeProperty.getType());
		queryWrapper.eq(ConfNodeProperty::getGraphId, confNodeProperty.getGraphId());
		if (confNodePropertyService.list().size() > 0) {
			LambdaUpdateWrapper<ConfNodeProperty> updateWrapper = Wrappers.lambdaUpdate();
			updateWrapper.eq(ConfNodeProperty::getNodeId, confNodeProperty.getNodeId());
			updateWrapper.eq(ConfNodeProperty::getType, confNodeProperty.getType());
			updateWrapper.eq(ConfNodeProperty::getGraphId, confNodeProperty.getGraphId());
			updateWrapper.set(ConfNodeProperty::getLocationX, confNodeProperty.getLocationX());
			updateWrapper.set(ConfNodeProperty::getLocationY, confNodeProperty.getLocationY());
			confNodeProperty.update(updateWrapper);
			return R.ok();
		}
		confNodeProperty.setCreateBy(SecurityUtils.getUser().getName());
		return R.ok(confNodePropertyService.save(confNodeProperty));
	}


	/**
	 * 获取节点位置
	 * @param confNodeProperty 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "获取节点位置", notes = "获取节点位置")
	@PostMapping("/getNodePosition")
	public R<List<ConfNodeProperty>> getPosition(@Validated(SelectFlag.class) @RequestBody ConfNodeProperty confNodeProperty) {
		return R.ok(confNodePropertyService.list(Wrappers.query(confNodeProperty)));
	}

	/**
	 * 新增节点数据
	 * @param confNodeProperty 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增节点数据", notes = "新增节点数据")
	@SysLog("新增节点数据")
	@PostMapping("/addNode")
	public R<Boolean> save(@Validated({UpdateFlag.class, Default.class}) @RequestBody ConfNodeProperty confNodeProperty) {
		return R.ok(confNodePropertyService.save(confNodeProperty));
	}

	/**
	 * 更新节点数据
	 * @param confNodeProperty 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "更新节点数据", notes = "更新节点数据")
	@PostMapping("/updateNode")
	public R<Boolean> queryByIds(@Validated({UpdateFlag.class, Default.class}) @RequestBody ConfNodeProperty confNodeProperty) {
		return R.ok(confNodePropertyService.update(Wrappers.query(confNodeProperty)));
	}

	/**
	 * 删除节点信息
	 * @param confNodeProperty 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "删除节点信息", notes = "删除节点信息")
	@SysLog("删除节点信息")
	@PostMapping("/deleteNode")
	public R<Boolean> removeById(@Validated @RequestBody ConfNodeProperty confNodeProperty){
		return R.ok(confNodePropertyService.remove(Wrappers.query(confNodeProperty)));
	}
}
